from basic import *
from sklearn.model_selection import train_test_split


def classify_1(sample,dataSet,labels,k=5):
	diffMat=(dataSet-sample)**2

	distance=diffMat.sum(axis=1)
	distance=distance**0.5

	sortedLabel=[labels[i] for i in distance.argsort()[:k]]
	classCount=collections.Counter(sortedLabel).most_common()
	return classCount[0][0]


def test(filename,ratio=0.1,k=5):
	mat,label,feature_name=file2fullMatrix(filename)
	normMat=autoNorm(mat)

	x_train,x_test,y_train,y_test=train_test_split(mat,label,test_size=ratio)

	
	sampleNum=x_test.shape[0]
	

	errorCount=0.0
	for i in range(0,sampleNum):
		result=classify_1(x_test[i],x_train,y_train,k)

		if result!=y_test[i]:
			errorCount+=1

	res=errorCount/sampleNum
	return res


if __name__ == '__main__':
	# filename="pd_speech_features.csv"
	# a=classify_1(mat[1],mat,label,4)
	filename="pd_speech_features.csv"
	res=test(filename,ratio=0.1,k=5)
	print("the error rate is:%f" %res)
	










